﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Collections.Generic;

using SBPweb.Forge.Contracts.Extensibility;

using SBPweb.PROvibe.Business.Shared.Contracts.Content;

namespace SBPweb.PROvibe.Presentation.Client.Controls.Containers
{

    public abstract class ContainerUserControl : UserControl, IContainer
    {

        #region Protected fields

        protected ICollection<ContainerDescriptor> containers = new List<ContainerDescriptor>();
        protected Dictionary<ContainerDescriptor, object> dictContents = new Dictionary<ContainerDescriptor, object>();
        private bool isInitialized = false;

        #endregion

        #region Property accessors

        public ICollection<ContainerDescriptor> Containers
        {
            get
            {
                return containers;
            }
        }

        public bool IsInitialized
        {
            get
            {
                return isInitialized;
            }
        }

        public IExtendable ExtendableApplication
        {
            get
            {
                return Application.Current as IExtendable;
            }
        }

        #endregion

        #region Methods

        public void AssignContainerContent(ContainerDescriptor container, object content)
        {
            if (!dictContents.ContainsKey(container))
            {
                dictContents.Add(container, content);
            }
            if (isInitialized)
            {
                SetContent(container, content);
            }
        }

        public void InitializeContents()
        {
            foreach (KeyValuePair<ContainerDescriptor, object> kvp in dictContents)
            {
                SetContent(kvp.Key, kvp.Value);
            }
            isInitialized = true;
        }

        protected abstract void SetContent(ContainerDescriptor container, object content);

        #endregion

    }

}